package demo.boat.boatSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.boat.boatSystem.entity.ShpVessels;
import demo.boat.boatSystem.mapper.ShpVesselsMapper;
import demo.boat.boatSystem.service.ShpVesselsService;
import demo.boat.boatSystem.vo.VesselsVO;
import demo.boat.response.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 船舶规范 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/vessels")
public class ShpVesselsController {

    @Resource
    private ShpVesselsService shpVesselsService;
    @Autowired
    private ShpVesselsMapper shpVesselsMapper;

    @PostMapping("/findvoyagesPage")
    public Result findvoyagesPage(@RequestParam(required = true, defaultValue = "1") Integer current,
                                   @RequestParam(required = true,defaultValue = "7") Integer size,
                                   @RequestBody VesselsVO vesselsVO) {
        Page<ShpVessels> page = new Page<>(current, size);
        QueryWrapper<ShpVessels> wrapper = getWrapper(vesselsVO);
        IPage<ShpVessels> voyagesPage = shpVesselsService.findvoyagesPage(page, wrapper);
        long total = voyagesPage.getTotal();
        List<ShpVessels> records = voyagesPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }
    private QueryWrapper<ShpVessels> getWrapper(VesselsVO vesselsVO) {
        QueryWrapper<ShpVessels> queryWrapper = new QueryWrapper<>();
        if (vesselsVO != null) {
            String shipName = vesselsVO.getStrInputName();
            if (!StringUtils.isEmpty(shipName) && checkcountname(shipName)) {
                queryWrapper.like("VLS_CHNNM", shipName).orderByDesc("VVY_ACTUAL_END_TM");
            }
            if (!StringUtils.isEmpty(shipName) && !checkcountname(shipName)) {
                queryWrapper.like("VLS_ENGNM", shipName).orderByDesc("VVY_ACTUAL_END_TM");
            }
        }
        return queryWrapper;
    }
    public boolean checkcountname(String countname)
    {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(countname);
        if (m.find()) {
            return true;
        }
        return false;
    }
}

